package cn.wolfcode.shop.cloud.web.advice;

import cn.wolfcode.shop.cloud.common.CommonControllerAdvice;
import cn.wolfcode.shop.cloud.common.Result;


import cn.wolfcode.shop.cloud.msg.MemberCodeMsg;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;


@ControllerAdvice
public class MemberControllerAdvice extends CommonControllerAdvice {

    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Result handlerMemberExp(BindException ex) {

        //手机号码格式不正确get(0)拿到索引为0的
        //new MemberCodeMsg(50104, "参数异常:{0}");,拿到胡括号中的错误信息msg
        String message = ex.getAllErrors().get(0).getDefaultMessage();
        //new MemberCodeMsg(50104, "参数异常:{0}");
        MemberCodeMsg pararmError = MemberCodeMsg.PARARM_ERROR;
        //{50104:"参数异常: 手机号码不正确"}
        return Result.error(pararmError.filterArg(message));
    }


}
